package com.wasion.gydpe.ice;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import CommProxy.CBasicParameter;
import CommProxy.IWriteablePrx;
import CommProxy.IWriteablePrxHelper;

public class CMultiPeer extends CSinglePeer {
	protected Map<String, Ice.ObjectPrx> _proxys;
	protected Lock _mutex;

	public CMultiPeer(IceWrapper iceWrapper) {
		super(iceWrapper);
		_mutex = new ReentrantLock();
		_proxys = new HashMap<String, Ice.ObjectPrx>();
	}

	public void addProxy(String proxyParam, Ice.ObjectPrx proxy) {
		_mutex.lock();
		_proxys.put(proxyParam, proxy);
		_mutex.unlock();
	}

	public int trans(CBasicParameter frame, boolean isWait) {
		int ret = CCustomReceiver.CP_UNKNOWN_ERROR;

		IWriteablePrx proxy;
		String receiver = frame.receiver;

		boolean isFind = true;

		if (receiver == null) {
			return super.trans(frame, isWait);
		} else {
			_mutex.lock();
			Ice.ObjectPrx p = _proxys.get(receiver);
			if (null != p) // 找到
			{
				proxy = IWriteablePrxHelper.uncheckedCast(p);
			} else
			// 没找到
			{
				isFind = false;
				TransRet r = stringToTransProxy(receiver, true); // 构造

				if (r.result != CCustomReceiver.CP_OK) {// 构造不成功
					return r.result;
				} else {
					proxy = r.proxy;
					_proxys.put(receiver, r.proxy);
				}
			}

			ret = send(frame, isWait, proxy);

			// 在本地找的的代理，怕过期
			// 如果使用ice底层发送的时候抛出异常，再构造重发一次
			if ((isFind) && (ret == CCustomReceiver.CP_ICE_EXCEPTION)) {
				// 重新构造代理再发一次
				TransRet r = stringToTransProxy(receiver, true);
				if (r.result == CCustomReceiver.CP_OK) {// 构造成功
					_proxys.put(receiver, r.proxy); // 更新代理
					ret = send(frame, isWait, r.proxy); // 重发
				}

			}
			_mutex.unlock();
		}

		return ret;
	}

	public int trans(CBasicParameter frame, boolean isWait, Object user) {
		int ret = CCustomReceiver.CP_UNKNOWN_ERROR;

		IWriteablePrx proxy;
		String receiver = frame.receiver;

		boolean isFind = true;

		if (receiver == null) {
			return super.trans(frame, isWait, user);
		} else {
			_mutex.lock();
			try {
				Ice.ObjectPrx p = _proxys.get(receiver);
				if (null != p) // 找到
				{
					proxy = IWriteablePrxHelper.uncheckedCast(p);
				} else
				// 没找到
				{
					isFind = false;
					TransRet r = stringToTransProxy(receiver, true); // 构造

					if (r.result != CCustomReceiver.CP_OK) {// 构造不成功

						return r.result;
					} else {
						proxy = r.proxy;
						_proxys.put(receiver, r.proxy);
					}
				}

				ret = send(frame, isWait, user, proxy);

				// 在本地找的的代理，怕过期
				// 如果使用ice底层发送的时候抛出异常，再构造重发一次
				if ((isFind) && (ret == CCustomReceiver.CP_ICE_EXCEPTION)) {
					// 重新构造代理再发一次
					TransRet r = stringToTransProxy(receiver, true);
					if (r.result == CCustomReceiver.CP_OK) {// 构造成功
						_proxys.put(receiver, r.proxy); // 更新代理
						ret = send(frame, isWait, r.proxy); // 重发
					}

				}

				return ret;
			} finally {

				_mutex.unlock();
			}
		}

	}
}
